import TimerManager, { ITimer } from "./manager/TimerManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Player extends cc.Component implements ITimer{
   
    // 主角跳跃高度
    @property(Number)
    private jumpHeight:number = 0;

    // 主角跳跃持续时间
    @property(Number)
    private jumpDuration:number = 0;

    // 最大移动速度
    @property(Number)
    private maxMoveSpeed:number = 0;

    // 加速度
    @property(Number)
    private accel:number = 0;

    accLeft: boolean;
    accRight: boolean;
    xSpeed: number;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

        // 初始化跳跃动作
        let jumpAction = this.setJumpAction();
        this.node.runAction(jumpAction);

         // 加速度方向开关
         this.accLeft = false;
         this.accRight = false;
         // 主角当前水平方向速度
         this.xSpeed = 0;
 
         // 初始化键盘输入监听
         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);  

         TimerManager.ins.addTimer(this);
    }

   
    public runMilliTimer():void
    {

    }

    private count:number = 0
    public runTimer():void
    {
        cc.warn(this.count++)
    }

    private setJumpAction()
    {
        // 跳跃上升
        var jumpUp = cc.moveBy(this.jumpDuration, cc.v2(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 下落
        var jumpDown = cc.moveBy(this.jumpDuration, cc.v2(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
        // 不断重复
        return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
    }

    private onKeyDown (event) 
    {
        // set a flag when key pressed
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = true;
                break;
            case cc.macro.KEY.d:
                this.accRight = true;
                break;
        }
    }

    private onKeyUp (event) 
    {
        // unset a flag when key released
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = false;
                break;
            case cc.macro.KEY.d:
                this.accRight = false;
                break;
        }
    }

    public onDestroy () 
    {
        // 取消键盘输入监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        TimerManager.ins.removeTimer(this);
    }

    update (dt)
    {
         // 根据当前加速度方向每帧更新速度
         if (this.accLeft) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) 
        {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;

        //超出边界处理
        if(this.node.x < -440)
        {
            this.node.x = -440;
        }
        if(this.node.x > 440)
        {
            this.node.x = 440;
        }
    }
}
